"""Build common libraries"""


COMMON_FILES = ['debug.c', 'mem.c', 'fail_loc.c', 'lru.c',
                'misc.c', 'pool_map.c', 'sort.c', 'btree.c', 'prop.c',
                'btree_class.c', 'tse.c', 'rsvc.c', 'checksum.c',
                'drpc.c', 'drpc.pb-c.c', 'proc.c',
                'acl_api.c', 'acl_util.c', 'acl_principal.c', 'cont_props.c',
                'dedup.c', 'profile.c', 'compression.c', 'compression_isal.c',
                'compression_qat.c', 'multihash.c', 'multihash_isal.c',
                'cipher.c', 'cipher_isal.c', 'qat.c', 'fault_domain.c',
                'tls.c', 'metrics.c', 'drpc_test.pb-c.c']


def build_daos_common(denv, client):
    """ Building non-pmem version for client's common lib"""
    benv = denv.Clone()
    ad_mem_files = []
    dav_src = []
    ult_files = []

    common_libs = ['isal', 'isal_crypto', 'cart', 'gurt', 'lz4', 'protobuf-c', 'uuid', 'pthread']
    if client:
        libname = 'daos_common'
    else:
        dav_src = ['dav/alloc_class.c', 'dav/bucket.c', 'dav/container_ravl.c',
                   'dav/container_seglists.c', 'dav/critnib.c', 'dav/dav_clogs.c',
                   'dav/dav_iface.c', 'dav/heap.c', 'dav/memblock.c',
                   'dav/memops.c', 'dav/os_thread_posix.c', 'dav/palloc.c', 'dav/ravl.c',
                   'dav/ravl_interval.c', 'dav/recycler.c', 'dav/stats.c', 'dav/tx.c', 'dav/ulog.c',
                   'dav/util.c', 'dav/wal_tx.c']
        ad_mem_files = ['ad_mem.c', 'ad_tx.c']
        ult_files = ['ult_errno.c']
        common_libs.extend(['pmemobj', 'abt', 'dav_v2'])
        benv.AppendUnique(RPATH_FULL=['$PREFIX/lib64/daos_srv'])
        benv.Append(CPPDEFINES=['-DDAOS_PMEM_BUILD'])
        benv.Append(OBJPREFIX="v_")
        libname = 'daos_common_pmem'
        benv.require('pmdk', 'argobots')

    common = benv.d_library(libname, COMMON_FILES + dav_src + ad_mem_files + ult_files,
                            LIBS=common_libs)
    benv.Install('$PREFIX/lib64/', common)
    return common


def scons():
    """Execute build"""
    Import('env', 'base_env', 'prereqs')

    env.AppendUnique(LIBPATH=[Dir('.')])
    base_env.AppendUnique(LIBPATH=[Dir('.')])
    base_env.d_add_build_rpath()
    env.d_add_build_rpath()

    denv = env.Clone()
    # generated protoc header should really move to this directory.
    denv.AppendUnique(CPPPATH=[Dir("../include/daos").srcnode()])
    denv.require('isal', 'isal_crypto', 'protobufc')
    denv.AppendUnique(LIBS=['cart', 'gurt', 'lz4'])
    build_daos_common(denv, True)

    control_tgts = [File('control.c')]
    Export('control_tgts')

    cmd_parser = denv.SharedObject(['cmd_parser.c'])
    Export('cmd_parser')
    dc_credit = denv.SharedObject(['credit.c'])
    Export('dc_credit')

    if prereqs.server_requested():
        SConscript('dav_v2/SConscript', exports=['denv'])
        build_daos_common(denv, False)

    if not prereqs.test_requested():
        return

    tlibenv = env.Clone(LIBS=[])
    tlibenv.require('argobots', 'isal', 'isal_crypto', 'protobufc')
    tlibenv.AppendUnique(LIBS=['cart', 'gurt', 'lz4', 'json-c'])

    tests_lib = tlibenv.d_library('daos_tests', ['tests_lib.c', 'tests_dmg_helpers.c'])

    tlibenv.Install('$PREFIX/lib64/', tests_lib)
    tenv = denv.Clone()

    if prereqs.server_requested():
        tenv.require('pmdk')

        tenv.Append(CPPDEFINES=['-DDAOS_PMEM_BUILD'])
        utest_utils = tenv.SharedObject('tests/utest_common.c')
        Export('utest_utils')

    SConscript('tests/SConscript', exports='tenv')


if __name__ == "SCons.Script":
    scons()
